package net.wanho.task;

import lombok.RequiredArgsConstructor;
import net.wanho.mapper.ProductMapper;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/1/1 15:17
 * @version: 1.0
 */

@Component
@RequiredArgsConstructor
public class OrderTask implements ApplicationContextAware {

    private final ProductMapper productMapper;
    private ApplicationContext context;

    //  每5秒执行一次
    @Scheduled(cron = "0 * * * * ?")
    private void processTimeoutOrder() {
        // 查询库存为0的商品
        List<Integer> productIds = productMapper.selectProductInvent();
        // 发送邮件提醒
        if (productIds.size() > 0) {
            System.out.println("您的商品库存不足！请及时补充！");
        }
    }


    // 每天凌晨1点检查
//    @Scheduled(cron = "0 0 1 * * ?")
//    public void processReceiveOrder() {
//        // 查询超过7天未收货的订单
//        List<Integer> ids = orderMapper.selectReceivePay(LocalDateTime.now().minusDays(7), OrderConstant.WAIT_RECEIVE);
//        // 修改订单状态为已收货
//        if (ids != null) {
//            orderMapper.updateReceivePay(ids, OrderConstant.COMPLETED);
//
//        }
//    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public ApplicationContext getContext() {
        return context;
    }

    public void setContext(ApplicationContext context) {
        this.context = context;
    }
}
